<?php

namespace App\Api\Controllers\Vbill;

use App\Api\Controllers\Config\MyBankConfigController;
use App\Api\Controllers\Config\TfConfigController;
use App\Api\Controllers\Config\VbillConfigController;
use App\Models\MemberList;
use App\Models\MemberSetJf;
use App\Models\MemberTpl;
use EasyWeChat\Factory;
use App\Models\Order;
use App\Models\Store;
use Illuminate\Http\Request;
use App\Api\Controllers\Config\WingConfigController;
use Illuminate\Support\Facades\DB;

class OauthController extends BaseController
{
    //授权
    public function oauth(Request $request)
    {
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        //随行付配置
        $config = new VbillConfigController();
        $vbill_config = $config->vbill_config($config_id);
        if (!$vbill_config) {
            return json_encode([
                'status' => 2,
                'message' => '随行付配置不存在请检查配置'
            ]);
        }


        $config = [
            'app_id' => $vbill_config->wx_appid,
            'scope' => 'snsapi_base',
            'oauth' => [
                'scopes' => ['snsapi_base'],
                'response_type' => 'code',
                'callback' => url('api/vbill/weixin/oauth_callback?sub_info=' . $sub_info . '&wx_AppId=' . $vbill_config->wx_appid . '&wx_Secret=' . $vbill_config->wx_secret . ''),
            ],

        ];
        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        return $oauth->redirect();

    }
    
    //授权
    public function icbc_oauth(Request $request)
    {
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        $code_url = url('api/vbill/weixin/icbc_oauth_callback?sub_info=' . $sub_info . '&wx_AppId=' . 111 . '&wx_Secret=' . 222 . '');

        return redirect($code_url);
    }
    
    public function icbc_oauth_callback(Request $request)
    {
        $sub_info = $request->get('sub_info');
        $code = $request->get('code');
        $wx_AppId = $request->get('wx_AppId');
        $wx_Secret = $request->get('wx_Secret');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        $store_id = $sub_info_arr['store_id'];
        $store_name = $sub_info_arr['store_name'];
        $merchant_id = $sub_info_arr['merchant_id'];

        $open_id = '111';

        $data = [
            'store_id' => $store_id,
            'store_name' => $store_name,
            'store_address' => '',
            'open_id' => $open_id,
            'merchant_id' => $merchant_id,
        ];
        echo '<pre>';
        $data = base64_encode(json_encode((array)$data));
        return redirect('/api/vbill/weixin/icbc_pay_view?data=' . $data);


    }
    
    //支付显示页面
    public function icbc_pay_view(Request $request)
    {
        $data = json_decode(base64_decode((string)$request->get('data')), true);

        $store_id = $data['store_id'];
        $open_id = $data['open_id'];
        return view('vbill.icbc_weixin', compact('data'));
    }
    
    //授权
    public function icbc_alioauth(Request $request)
    {
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        $code_url = url('api/vbill/weixin/icbc_alioauth_callback?sub_info=' . $sub_info . '&wx_AppId=' . 111 . '&wx_Secret=' . 222 . '');

        return redirect($code_url);
    }
    
    public function icbc_alioauth_callback(Request $request)
    {
        $sub_info = $request->get('sub_info');
        $code = $request->get('code');
        $wx_AppId = $request->get('wx_AppId');
        $wx_Secret = $request->get('wx_Secret');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        $store_id = $sub_info_arr['store_id'];
        $store_name = $sub_info_arr['store_name'];
        $merchant_id = $sub_info_arr['merchant_id'];

        $open_id = '111';

        $data = [
            'store_id' => $store_id,
            'store_name' => $store_name,
            'store_address' => '',
            'open_id' => $open_id,
            'merchant_id' => $merchant_id,
        ];
        $data = base64_encode(json_encode((array)$data));
        return redirect('/api/vbill/weixin/icbc_alipay_view?data=' . $data);
    }
    
    //支付显示页面
    public function icbc_alipay_view(Request $request)
    {
        $data = json_decode(base64_decode((string)$request->get('data')), true);

        $store_id = $data['store_id'];
        $open_id = $data['open_id'];
        return view('vbill.icbc_alipay', compact('data'));
    }
    
    //邮储银行 微信主扫
    public function youpayoauth(Request $request){
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        return redirect(url('api/vbill/weixin/youpayoauth_callback?sub_info=' . $sub_info . '&wx_AppId=' . '000000' . '&wx_Secret=' . '000000' . ''));
    }
    
    public function youpayoauth_callback(Request $request){
        $sub_info = $request->get('sub_info');
        $code = $request->get('code');
        $wx_AppId = $request->get('wx_AppId');
        $wx_Secret = $request->get('wx_Secret');

        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        $store_id = $sub_info_arr['store_id'];
        $store_name = $sub_info_arr['store_name'];
        $merchant_id = $sub_info_arr['merchant_id'];
        
        $ad=DB::table('ads')->where('store_key_ids', $store_id)->first();
        
        $data = [
            'store_id' => $store_id,
            'store_name' => $store_name,
            'store_address' => '',
            'open_id' => '000000',
            'merchant_id' => $merchant_id,
            'ad_url' => isset($ad->ad_url)?$ad->ad_url:'',
        ];
        $data = base64_encode(json_encode((array)$data));
        return redirect('/api/vbill/weixin/youpay1_view?data=' . $data);
    }
    
    //支付显示页面
    public function youpay1_view(Request $request){
        $data = json_decode(base64_decode((string)$request->get('data')), true);

        $store_id = $data['store_id'];
        $open_id = $data['open_id'];
        
        
        
         $ret = \Illuminate\Support\Facades\DB::table('source_config')
            ->where(['store_id' => $data['store_id']])
            ->where(['company' => 'psbc'])
            ->where(['status' => 1])
            ->first();
        if (isset($ret->url) && !empty($ret->url)) {
            $data['suyuan'] = 1;
            $data['suyuan_url'] = $ret->url;
            return view('vbill.source', compact('data'));
        } else {
            return view('vbill.youpayweixin', compact('data'));
        }

        //return view('vbill.youpayweixin', compact('data'));
    }
    
    //支付显示页面
    public function user_payqr(Request $request){
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $data = base64_encode(json_encode((array)$sub_info_arr));
        $data = json_decode(base64_decode((string)$data), true);
        // $store_id = $data['store_id'];
        // $open_id = $data['open_id'];

        return view('vbill.user_qr', compact('data'));
    }
    
     //授权
    public function oauth1(Request $request)
    {
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        //随行付配置
        $config = new VbillConfigController();
        $vbill_config = $config->vbill_config($config_id);
        if (!$vbill_config) {
            return json_encode([
                'status' => 2,
                'message' => '随行付配置不存在请检查配置'
            ]);
        }


        $config = [
            'app_id' => $vbill_config->wx_appid,
            'scope' => 'snsapi_base',
            'oauth' => [
                'scopes' => ['snsapi_base'],
                'response_type' => 'code',
                'callback' => url('api/vbill/weixin/oauth_callback1?sub_info=' . $sub_info . '&wx_AppId=' . $vbill_config->wx_appid . '&wx_Secret=' . $vbill_config->wx_secret . ''),
            ],

        ];
        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        return $oauth->redirect();

    }
    //翼支付支付
    public function wingpay(Request $request){
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $store_id = $sub_info_arr['store_id'];
        $store_pid=0;
        $wing_config = new WingConfigController();
        $h_merchant = $wing_config->wing_merchant($store_id, $store_pid);
        if(empty($h_merchant)){
            $pid = Store::where('store_id', $sub_info_arr['store_id'])->select('pid')->first();
            $store_pid=$pid->pid;
            
            $h_merchant = $wing_config->wing_merchant($store_id, $store_pid);
        }
        $data = [
            'store_id' => $sub_info_arr['store_id'],
            'store_name' => $sub_info_arr['store_name'],
            'store_address' => '',
            'open_id' => '',
            'merchant_id' => $sub_info_arr['merchant_id'],
            'merchantNo' => $h_merchant->merchantNo,
        ];
        
        $is_member = 0;
        $MemberTpl = MemberTpl::where('store_id', $store_id)
            ->select('tpl_status')
            ->first();
        if ($MemberTpl && $MemberTpl->tpl_status == 1) {
            $is_member = 1;
        }

        //如果是会员
        if (0) {
            //判断是否是会员
            $MemberList = MemberList::where('store_id', $store_id)
                ->where('wx_openid', $open_id)
                ->select('mb_jf', 'mb_money', 'mb_id')
                ->first();
            $data['mb_jf'] = "";
            $data['mb_id'] = "";
            $data['mb_money'] = "";
            $data['dk_money'] = "";
            $data['dk_jf'] = "0";
            $data['ways_source'] = "weixin";

            if ($MemberList) {
                $data['mb_jf'] = $MemberList->mb_jf;
                $data['mb_id'] = $MemberList->mb_id;
                $data['mb_money'] = $MemberList->mb_money;
                //判断是否有积分抵扣
                $MemberSetJf = MemberSetJf::where('store_id', $store_id)
                    ->select('dk_jf_m', 'dk_rmb')
                    ->first();

                if ($MemberSetJf) {
                    //3.用户的积分一共可以抵扣多少钱
                    $data['dk_money'] = ($MemberList->mb_jf / $MemberSetJf->dk_jf_m) * $MemberSetJf->dk_rmb;
                    $data['dk_jf'] = $MemberList->mb_jf;
                }

            }
            $data['ways_type'] = "13002";
            $data['company'] = "vbill";
            return view('vbill.membermweixin', compact('data'));
        } else {
            return view('vbill.wingpay', compact('data'));
        }
    }
    
    //商户充值
    public function merch_qr(Request $request){
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $store_id = $sub_info_arr['store_id'];
        $data = [
            'store_id' => $sub_info_arr['store_id'],
            'store_name' => $sub_info_arr['store_name'],
            'store_address' => '',
            'open_id' => '',
        ];
        return view('vbill.merch_qr', compact('data'));
    }
    
    //翼支付 银联支付
    public function unwingpay(Request $request){
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $store_id = $sub_info_arr['store_id'];
        $store_pid=0;
        $wing_config = new WingConfigController();
        $h_merchant = $wing_config->wing_merchant($store_id, $store_pid);
        if(empty($h_merchant)){
            $pid = Store::where('store_id', $sub_info_arr['store_id'])->select('pid')->first();
            $store_pid=$pid->pid;
            
            $h_merchant = $wing_config->wing_merchant($store_id, $store_pid);
        }
        $data = [
            'store_id' => $sub_info_arr['store_id'],
            'store_name' => $sub_info_arr['store_name'],
            'store_address' => '',
            'open_id' => '',
            'merchant_id' => $sub_info_arr['merchant_id'],
            'merchantNo' => $h_merchant->merchantNo,
        ];
        $is_member = 0;
        $MemberTpl = MemberTpl::where('store_id', $store_id)
            ->select('tpl_status')
            ->first();
        if ($MemberTpl && $MemberTpl->tpl_status == 1) {
            $is_member = 1;
        }

        //如果是会员
        if (0) {
            //判断是否是会员
            $MemberList = MemberList::where('store_id', $store_id)
                ->where('wx_openid', $open_id)
                ->select('mb_jf', 'mb_money', 'mb_id')
                ->first();
            $data['mb_jf'] = "";
            $data['mb_id'] = "";
            $data['mb_money'] = "";
            $data['dk_money'] = "";
            $data['dk_jf'] = "0";
            $data['ways_source'] = "weixin";

            if ($MemberList) {
                $data['mb_jf'] = $MemberList->mb_jf;
                $data['mb_id'] = $MemberList->mb_id;
                $data['mb_money'] = $MemberList->mb_money;
                //判断是否有积分抵扣
                $MemberSetJf = MemberSetJf::where('store_id', $store_id)
                    ->select('dk_jf_m', 'dk_rmb')
                    ->first();

                if ($MemberSetJf) {
                    //3.用户的积分一共可以抵扣多少钱
                    $data['dk_money'] = ($MemberList->mb_jf / $MemberSetJf->dk_jf_m) * $MemberSetJf->dk_rmb;
                    $data['dk_jf'] = $MemberList->mb_jf;
                }

            }
            $data['ways_type'] = "13002";
            $data['company'] = "vbill";
            return view('vbill.membermweixin', compact('data'));
        } else {
            return view('vbill.unwingpay', compact('data'));
        }
    }
    
    //平安银行 银联支付
    public function unsafepay(Request $request){
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $store_id = $sub_info_arr['store_id'];
        $store_pid=0;
        $data = [
            'store_id' => $sub_info_arr['store_id'],
            'store_name' => $sub_info_arr['store_name'],
            'store_address' => '',
            'open_id' => '',
            'merchant_id' => $sub_info_arr['merchant_id']
        ];

        return view('vbill.unsafepay', compact('data'));
    }
    
    //邮储银行 银联支付
    public function unyoupay(Request $request){
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $store_id = $sub_info_arr['store_id'];
        $store_pid=0;
        $data = [
            'store_id' => $sub_info_arr['store_id'],
            'store_name' => $sub_info_arr['store_name'],
            'store_address' => '',
            'open_id' => '',
            'merchant_id' => $sub_info_arr['merchant_id']
        ];

        return view('vbill.unyoupay', compact('data'));
    }
    
    //翼支付
    public function wingoauth(Request $request){
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        //随行付配置
        $config = new VbillConfigController();
        $vbill_config = $config->vbill_config($config_id);
        if (!$vbill_config) {
            return json_encode([
                'status' => 2,
                'message' => '随行付配置不存在请检查配置'
            ]);
        }


        $config = [
            'app_id' => $vbill_config->wx_appid,
            'scope' => 'snsapi_base',
            'oauth' => [
                'scopes' => ['snsapi_base'],
                'response_type' => 'code',
                'callback' => url('api/vbill/weixin/wingoauth_callback?sub_info=' . $sub_info . '&wx_AppId=' . $vbill_config->wx_appid . '&wx_Secret=' . $vbill_config->wx_secret . ''),
            ],

        ];
        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        return $oauth->redirect();
    }
    
    //平安银行
    public function safeoauth(Request $request){
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        //随行付配置
        $config = new VbillConfigController();
        $vbill_config = $config->vbill_config($config_id);
        if (!$vbill_config) {
            return json_encode([
                'status' => 2,
                'message' => '随行付配置不存在请检查配置'
            ]);
        }


        $config = [
            'app_id' => $vbill_config->wx_appid,
            'scope' => 'snsapi_base',
            'oauth' => [
                'scopes' => ['snsapi_base'],
                'response_type' => 'code',
                'callback' => url('api/vbill/weixin/safeoauth_callback?sub_info=' . $sub_info),
            ],

        ];
        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        return $oauth->redirect();
    }
    
    public function wingoauth_callback(Request $request)
    {
        $sub_info = $request->get('sub_info');
        $code = $request->get('code');
        $wx_AppId = $request->get('wx_AppId');
        $wx_Secret = $request->get('wx_Secret');

        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        $store_id = $sub_info_arr['store_id'];
        $store_name = $sub_info_arr['store_name'];
        $merchant_id = $sub_info_arr['merchant_id'];

        $config = [
            'app_id' => $wx_AppId,
            "secret" => $wx_Secret,
            "code" => $code,
            "grant_type" => "authorization_code",
        ];

        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        $user = $oauth->user();
        $open_id = $user->getId();
        
        $ad=DB::table('ads')->where('store_key_ids', $store_id)->first();

        $data = [
            'store_id' => $store_id,
            'store_name' => $store_name,
            'store_address' => '',
            'open_id' => $open_id,
            'merchant_id' => $merchant_id,
            'ad_url' => isset($ad->ad_url)?$ad->ad_url:'',
        ];

        $data = base64_encode(json_encode((array)$data));
        return redirect('/api/vbill/weixin/wingpay_view?data=' . $data);


    }
    
    public function safeoauth_callback(Request $request)
    {
        $sub_info = $request->get('sub_info');
        $code = $request->get('code');

        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        $store_id = $sub_info_arr['store_id'];
        $store_name = $sub_info_arr['store_name'];
        $merchant_id = $sub_info_arr['merchant_id'];
        // $wx_AppId = 1;
        // $wx_Secret = 1;

        // $config = [
        //     'app_id' => $wx_AppId,
        //     "secret" => $wx_Secret,
        //     "code" => $code,
        //     "grant_type" => "authorization_code",
        // ];

        // $app = Factory::officialAccount($config);
        // $oauth = $app->oauth;
        // $user = $oauth->user();
        // $open_id = $user->getId();

        $data = [
            'store_id' => $store_id,
            'store_name' => $store_name,
            'store_address' => '',
            'open_id' => 1,
            'merchant_id' => $merchant_id,
        ];
        $data = base64_encode(json_encode((array)$data));
        return redirect('/api/vbill/weixin/safepay_view?data=' . $data);


    }

    public function oauth_callback(Request $request)
    {
        $sub_info = $request->get('sub_info');
        $code = $request->get('code');
        $wx_AppId = $request->get('wx_AppId');
        $wx_Secret = $request->get('wx_Secret');

        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        $store_id = $sub_info_arr['store_id'];
        $store_name = $sub_info_arr['store_name'];
        $merchant_id = $sub_info_arr['merchant_id'];

        $config = [
            'app_id' => $wx_AppId,
            "secret" => $wx_Secret,
            "code" => $code,
            "grant_type" => "authorization_code",
        ];

        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        $user = $oauth->user();
        $open_id = $user->getId();
        $ad=DB::table('ads')->where('store_key_ids', $store_id)->first();

        $data = [
            'store_id' => $store_id,
            'store_name' => $store_name,
            'store_address' => '',
            'open_id' => $open_id,
            'merchant_id' => $merchant_id,
            'ad_url' => isset($ad->ad_url)?$ad->ad_url:'',
        ];

        $data = base64_encode(json_encode((array)$data));
        return redirect('/api/vbill/weixin/pay_view?data=' . $data);
    }
    
    public function oauth_callback1(Request $request)
    {
        $sub_info = $request->get('sub_info');
        $code = $request->get('code');
        $wx_AppId = $request->get('wx_AppId');
        $wx_Secret = $request->get('wx_Secret');

        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        $store_id = $sub_info_arr['store_id'];
        $store_name = $sub_info_arr['store_name'];
        $merchant_id = $sub_info_arr['merchant_id'];

        $config = [
            'app_id' => $wx_AppId,
            "secret" => $wx_Secret,
            "code" => $code,
            "grant_type" => "authorization_code",
        ];

        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        $user = $oauth->user();
        $open_id = $user->getId();

        $data = [
            'store_id' => $store_id,
            'store_name' => $store_name,
            'store_address' => '',
            'open_id' => $open_id,
            'merchant_id' => $merchant_id,
        ];

        $data = base64_encode(json_encode((array)$data));
        return redirect('/api/vbill/weixin/pay_view1?data=' . $data);
    }

    //授权
    public function oautha(Request $request)
    {
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        //随行付配置
        $config = new VbillConfigController();
        $vbill_config = $config->vbilla_config($config_id);
        if (!$vbill_config) {
            return json_encode([
                'status' => 2,
                'message' => '随行付配置不存在请检查配置'
            ]);
        }


        $config = [
            'app_id' => $vbill_config->wx_appid,
            'scope' => 'snsapi_base',
            'oauth' => [
                'scopes' => ['snsapi_base'],
                'response_type' => 'code',
                'callback' => url('api/vbill/weixin/oauth_callbacka?sub_info=' . $sub_info . '&wx_AppId=' . $vbill_config->wx_appid . '&wx_Secret=' . $vbill_config->wx_secret . ''),
            ],

        ];
        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        return $oauth->redirect();

    }

    public function oauth_callbacka(Request $request)
    {
        $sub_info = $request->get('sub_info');
        $code = $request->get('code');
        $wx_AppId = $request->get('wx_AppId');
        $wx_Secret = $request->get('wx_Secret');

        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $config_id = $sub_info_arr['config_id'];
        $store_id = $sub_info_arr['store_id'];
        $store_name = $sub_info_arr['store_name'];
        $merchant_id = $sub_info_arr['merchant_id'];

        $config = [
            'app_id' => $wx_AppId,
            "secret" => $wx_Secret,
            "code" => $code,
            "grant_type" => "authorization_code",
        ];

        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        $user = $oauth->user();
        $open_id = $user->getId();

        $data = [
            'store_id' => $store_id,
            'store_name' => $store_name,
            'store_address' => '',
            'open_id' => $open_id,
            'merchant_id' => $merchant_id,
        ];

        $data = base64_encode(json_encode((array)$data));
        return redirect('/api/vbill/weixin/pay_view_a?data=' . $data);


    }

    //支付显示页面
    public function pay_view(Request $request)
    {
        $data = json_decode(base64_decode((string)$request->get('data')), true);


        $store_id = $data['store_id'];
        $open_id = $data['open_id'];
        $data['is_goods'] = Store::where('store_id',$store_id)->value('is_goods');

        $data['store_btn'] = json_decode(json_encode(DB::table('store_goods_btn')->where('store_id',$store_id)->first()), true);
        //查询是否有开启会员
        $is_member = 0;
        $MemberTpl = MemberTpl::where('store_id', $store_id)
            ->select('tpl_status')
            ->first();
        if ($MemberTpl && $MemberTpl->tpl_status == 1) {
            $is_member = 1;
        }

        //如果是会员
        if (0) {
            //判断是否是会员
            $MemberList = MemberList::where('store_id', $store_id)
                ->where('wx_openid', $open_id)
                ->select('mb_jf', 'mb_money', 'mb_id')
                ->first();
            $data['mb_jf'] = "";
            $data['mb_id'] = "";
            $data['mb_money'] = "";
            $data['dk_money'] = "";
            $data['dk_jf'] = "0";
            $data['ways_source'] = "weixin";

            if ($MemberList) {
                $data['mb_jf'] = $MemberList->mb_jf;
                $data['mb_id'] = $MemberList->mb_id;
                $data['mb_money'] = $MemberList->mb_money;
                //判断是否有积分抵扣
                $MemberSetJf = MemberSetJf::where('store_id', $store_id)
                    ->select('dk_jf_m', 'dk_rmb')
                    ->first();

                if ($MemberSetJf) {
                    //3.用户的积分一共可以抵扣多少钱
                    $data['dk_money'] = ($MemberList->mb_jf / $MemberSetJf->dk_jf_m) * $MemberSetJf->dk_rmb;
                    $data['dk_jf'] = $MemberList->mb_jf;
                }

            }
            $data['ways_type'] = "13002";
            $data['company'] = "vbill";
            return view('vbill.membermweixin', compact('data'));
        } else {
            return view('vbill.weixin', compact('data'));
        }


    }
    
    //支付显示页面
    public function pay_view1(Request $request)
    {
        $data = json_decode(base64_decode((string)$request->get('data')), true);


        $store_id = $data['store_id'];
        $open_id = $data['open_id'];
        //查询是否有开启会员
        $is_member = 0;
        $MemberTpl = MemberTpl::where('store_id', $store_id)
            ->select('tpl_status')
            ->first();
        if ($MemberTpl && $MemberTpl->tpl_status == 1) {
            $is_member = 1;
        }
        return view('vbill.wxmerch_qr', compact('data'));
    }
    
    //支付显示页面
    public function storelist(Request $request)
    {
        // $data = json_decode(base64_decode((string)$request->get('data')), true);

        // $store_id = $data['store_id'];
        $data=DB::table('stores')->where('pid','0')->get();
        return view('vbill.storelist', compact('data'));
    }
    
    //支付显示页面
    public function wingpay_view(Request $request)
    {
        $data = json_decode(base64_decode((string)$request->get('data')), true);


        $store_id = $data['store_id'];
        $open_id = $data['open_id'];
        //查询是否有开启会员
        $is_member = 0;
        $MemberTpl = MemberTpl::where('store_id', $store_id)
            ->select('tpl_status')
            ->first();
        if ($MemberTpl && $MemberTpl->tpl_status == 1) {
            $is_member = 1;
        }

        //如果是会员
        if (0) {
            //判断是否是会员
            $MemberList = MemberList::where('store_id', $store_id)
                ->where('wx_openid', $open_id)
                ->select('mb_jf', 'mb_money', 'mb_id')
                ->first();
            $data['mb_jf'] = "";
            $data['mb_id'] = "";
            $data['mb_money'] = "";
            $data['dk_money'] = "";
            $data['dk_jf'] = "0";
            $data['ways_source'] = "weixin";

            if ($MemberList) {
                $data['mb_jf'] = $MemberList->mb_jf;
                $data['mb_id'] = $MemberList->mb_id;
                $data['mb_money'] = $MemberList->mb_money;
                //判断是否有积分抵扣
                $MemberSetJf = MemberSetJf::where('store_id', $store_id)
                    ->select('dk_jf_m', 'dk_rmb')
                    ->first();

                if ($MemberSetJf) {
                    //3.用户的积分一共可以抵扣多少钱
                    $data['dk_money'] = ($MemberList->mb_jf / $MemberSetJf->dk_jf_m) * $MemberSetJf->dk_rmb;
                    $data['dk_jf'] = $MemberList->mb_jf;
                }

            }
            $data['ways_type'] = "13002";
            $data['company'] = "vbill";
            return view('vbill.membermweixin', compact('data'));
        } else {
            return view('vbill.wingweixin', compact('data'));
        }


    }
    
    //支付显示页面
    public function safepay_view(Request $request)
    {
        $data = json_decode(base64_decode((string)$request->get('data')), true);


        $store_id = $data['store_id'];
        $open_id = $data['open_id'];
        //查询是否有开启会员
        $is_member = 0;
        $MemberTpl = MemberTpl::where('store_id', $store_id)
            ->select('tpl_status')
            ->first();
        if ($MemberTpl && $MemberTpl->tpl_status == 1) {
            $is_member = 1;
        }
        return view('vbill.safeweixin', compact('data'));
    }
    
    //支付显示页面
    public function pay_view_a(Request $request)
    {
        $data = json_decode(base64_decode((string)$request->get('data')), true);


        $store_id = $data['store_id'];
        $open_id = $data['open_id'];
        //查询是否有开启会员
        $is_member = 0;
        $MemberTpl = MemberTpl::where('store_id', $store_id)
            ->select('tpl_status')
            ->first();
        if ($MemberTpl && $MemberTpl->tpl_status == 1) {
            $is_member = 1;
        }

        //如果是会员
        if (0) {
            //判断是否是会员
            $MemberList = MemberList::where('store_id', $store_id)
                ->where('wx_openid', $open_id)
                ->select('mb_jf', 'mb_money', 'mb_id')
                ->first();
            $data['mb_jf'] = "";
            $data['mb_id'] = "";
            $data['mb_money'] = "";
            $data['dk_money'] = "";
            $data['dk_jf'] = "0";
            $data['ways_source'] = "weixin";

            if ($MemberList) {
                $data['mb_jf'] = $MemberList->mb_jf;
                $data['mb_id'] = $MemberList->mb_id;
                $data['mb_money'] = $MemberList->mb_money;
                //判断是否有积分抵扣
                $MemberSetJf = MemberSetJf::where('store_id', $store_id)
                    ->select('dk_jf_m', 'dk_rmb')
                    ->first();

                if ($MemberSetJf) {
                    //3.用户的积分一共可以抵扣多少钱
                    $data['dk_money'] = ($MemberList->mb_jf / $MemberSetJf->dk_jf_m) * $MemberSetJf->dk_rmb;
                    $data['dk_jf'] = $MemberList->mb_jf;
                }

            }
            $data['ways_type'] = "19002";
            $data['company'] = "vbilla";
            return view('vbilla.membermweixin', compact('data'));
        } else {
            return view('vbilla.weixin', compact('data'));
        }


    }

    //微信充值页面
    public function member_cz_pay_view(Request $request)
    {
        return view('member.cz');

    }

    //第三方非想用平台来获取快钱的openid
    public function oauth_openid(Request $request)
    {
        $sub_info = $request->get('state');
        //第三方传过来的信息
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        //快钱配置
        $mbconfig = new MyBankConfigController();
        $MyBankConfig = $mbconfig->MyBankConfig('1234');
        $config = [
            'app_id' => $MyBankConfig->wx_AppId,
            'scope' => 'snsapi_base',
            'oauth' => [
                'scopes' => ['snsapi_base'],
                'response_type' => 'code',
                'callback' => url('api/vbill/weixin/oauth_callback_openid?sub_info=' . $sub_info . '&wx_AppId=' . $MyBankConfig->wx_AppId . '&wx_Secret=' . $MyBankConfig->wx_Secret . ''),
            ],

        ];
        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        return $oauth->redirect();

    }

    //第三方非想用平台来获取快钱的openid
    public function oauth_callback_openid(Request $request)
    {
        $sub_info = $request->get('sub_info');
        $code = $request->get('code');
        $wx_AppId = $request->get('wx_AppId');
        $wx_Secret = $request->get('wx_Secret');

        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);


        $config = [
            'app_id' => $wx_AppId,
            "secret" => $wx_Secret,
            "code" => $code,
            "grant_type" => "authorization_code",
        ];

        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        $user = $oauth->user();
        $open_id = $user->getId();

        $sub_info_arr['open_id'] = $open_id;
        $sub_info_arr['store_address'] = '';
        $callback_url = $sub_info_arr['callback_url'];

        $data = base64_encode(json_encode((array)$sub_info_arr));
        return redirect($callback_url . '?data=' . $data);


    }

    //第三方非想用平台来获取快钱的openid
    public function oauth_openida(Request $request)
    {
        $sub_info = $request->get('state');
        //第三方传过来的信息
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        //快钱配置
        $mbconfig = new MyBankConfigController();
        $MyBankConfig = $mbconfig->MyBankConfig('1234');
        $config = [
            'app_id' => $MyBankConfig->wx_AppId,
            'scope' => 'snsapi_base',
            'oauth' => [
                'scopes' => ['snsapi_base'],
                'response_type' => 'code',
                'callback' => url('api/vbill/weixin/oauth_callback_openida?sub_info=' . $sub_info . '&wx_AppId=' . $MyBankConfig->wx_AppId . '&wx_Secret=' . $MyBankConfig->wx_Secret . ''),
            ],

        ];
        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        return $oauth->redirect();

    }

    //第三方非想用平台来获取快钱的openid
    public function oauth_callback_openida(Request $request)
    {
        $sub_info = $request->get('sub_info');
        $code = $request->get('code');
        $wx_AppId = $request->get('wx_AppId');
        $wx_Secret = $request->get('wx_Secret');

        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);


        $config = [
            'app_id' => $wx_AppId,
            "secret" => $wx_Secret,
            "code" => $code,
            "grant_type" => "authorization_code",
        ];

        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;
        $user = $oauth->user();
        $open_id = $user->getId();

        $sub_info_arr['open_id'] = $open_id;
        $sub_info_arr['store_address'] = '';
        $callback_url = $sub_info_arr['callback_url'];

        $data = base64_encode(json_encode((array)$sub_info_arr));
        return redirect($callback_url . '?data=' . $data);


    }


    public function Options()
    {
        $options = [
            'app_id' => '',
            'payment' => [
                'merchant_id' => '',
                'key' => '',
                'cert_path' => '', // XXX: 绝对路径！！！！
                'key_path' => '',      // XXX: 绝对路径！！！！
                'notify_url' => '',       // 你也可以在下单时单独设置来想覆盖它
            ],
        ];

        return $options;
    }
}
